from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support.expected_conditions import presence_of_element_located
import time
import random

# with webdriver.Chrome() as driver:
driver = webdriver.Chrome()


# 点击出按钮
def login():
    wait = WebDriverWait(driver, 10)
    driver.get("https://10.163.236.173:8443/login/#/")
    driver.find_element(By.ID, "details-button").click()
    driver.find_element(By.ID, "proceed-link").click()
    time.sleep(5)
    # driver.current_window_handle  # 获取当前窗口
    # driver.window_handles  # 获取所有窗口
    driver.switch_to.window(driver.current_window_handle)  # 切换到指定窗口

    driver.find_element(By.XPATH, "//*[@id=\"inputUsername\"]").send_keys("admin" + Keys.RETURN)
    driver.find_element(By.XPATH, "//*[@id=\"inputPassword\"]").send_keys("Passw0rd123" + Keys.RETURN)
    time.sleep(5)


def clickButton():
    try:
        driver.find_element(By.XPATH, "/html/body/div[1]/taf-login/div/div/form/fieldset/div[5]/button").click()
    except Exception as err:
        print(1, err)
    time.sleep(5)
    # first_result = wait.until(presence_of_element_located((By.CSS_SELECTOR, "vendor-auth-widget-button-send-sms")))
    # print(first_result.get_attribute("textContent"))
    # time.sleep(5)
    driver.find_element(By.XPATH,
                        "/html/body/div[1]/taf-core-layout/div[2]/div[2]/taf-pivot-bar/nav/taf-nav-item[4]/a").click()
    time.sleep(5)


# 点击第一个IP门禁
def click_ip_control_menu():
    # ... 菜单点击出授权菜单
    driver.find_element(By.XPATH,
                        "/html/body/div[1]/taf-core-layout/div[2]/div[4]/taf-content/div/taf-rdum-cctv-content-panel/div/taf-rdum-dooraccess-monitor/div/div[1]/taf-rdum-dooraccess-engine-list/div/fieldset/div/div[2]/div[1]/div[1]").click()
    time.sleep(1)


# 点击门1的授权菜单
def click_door_auth_menu():
    # ... 菜单点击出授权菜单
    driver.find_element(By.XPATH,
                        "/html/body/div[1]/taf-core-layout/div[2]/div[4]/taf-content/div/taf-rdum-cctv-content-panel/div/taf-rdum-dooraccess-monitor/div/div[1]/taf-rdum-dooraccess-engine-list/div/fieldset/div/div[2]/div[3]/div[1]/div/button/span").click()
    time.sleep(1)


def sendOpenDoor():
    # 控制菜单
    driver.find_element(By.XPATH,
                        "/html/body/div[1]/taf-core-layout/div[2]/div[4]/taf-content/div/taf-rdum-cctv-content-panel/div/taf-rdum-dooraccess-monitor/div/div[2]/taf-rdum-dooraccess-datapoints/div/taf-tab/div/taf-tab-normal/div/div/ul/li[3]/a/uib-tab-heading/div/span").click()
    time.sleep(2)
    count = 1;
    while (True):
        try:
            driver.find_element(By.XPATH,
                                "/html/body/div[1]/taf-core-layout/div[2]/div[4]/taf-content/div/taf-rdum-cctv-content-panel/div/taf-rdum-dooraccess-monitor"
                                "/div/div[2]/taf-rdum-dooraccess-datapoints/div/taf-tab/div/taf-tab-normal/div/div/div/div[3]/"
                                "div/taf-rdum-dooraccess-control-tab/taf-table/div/div[3]/table/tbody[2]/tr[1]/table-actions/div/button").click()
            time.sleep(1)
            driver.find_element(By.XPATH,
                                "/html/body/div[1]/taf-core-layout/div[2]/div[4]/taf-content/div/taf-rdum-cctv-content-panel/div/taf-rdum-dooraccess-monitor/div/div[2]/taf-rdum-dooraccess-datapoints/div/taf-tab/div/taf-tab-normal/div/div/div/div[3]/div/taf-rdum-dooraccess-control-tab/taf-table/div/div[3]/table/tbody[2]/tr[1]/table-actions/div/ul/button").click()
            time.sleep(1)
            driver.find_element(By.XPATH,
                                "/html/body/div[1]/div/div/taf-modal-dialog/div/div[2]/taf-rdum-dooraccess-control-send-command/form/fieldset/fieldset/button[2]/div").click()
            time.sleep(3)
        except Exception as err:
            print(err)
            exit(0)
            # time.sleep(1)
            # driver.find_element(By.XPATH,
            #                     "/html/body/div[1]/div/div/taf-modal-dialog/div/div[2]/taf-rdum-dooraccess-control-send-command/form/fieldset/fieldset/button[2]/div").click()
            # time.sleep(3)
        finally:
            count += 1
            print(count)


# 指纹授权
def auth_button():
    # 授权按钮点击
    driver.find_element(By.XPATH,
                        "/html/body/div[1]/taf-core-layout/div[2]/div[4]/taf-content/div/taf-rdum-cctv-content-panel/div/taf-rdum-dooraccess-monitor/div/div[1]/taf-rdum-dooraccess-engine-list/div/fieldset/div/div[2]/div[3]/div[1]/div/ul/button[3]/span").click()
    time.sleep(1)
    em = driver.find_element(By.XPATH,
                             "/html/body/div[1]/div/div/taf-modal-dialog/div/div[2]/taf-rdum-dooraccess-door-authority/form/taf-table/div/div[3]/table/tbody[2]/tr/td[5]/div/select")
    count = 1
    while True:
        # em.click()
        # time.sleep(1)
        time.sleep(1)
        ws = em.find_elements_by_tag_name("option")
        ws[random.randint(0, 4)].click();
        time.sleep(3)
        try:
            result = driver.find_element(By.XPATH,
                                         "/html/body/div[1]/div/div/taf-modal-dialog/div/div[2]/taf-rdum-dooraccess-door-authority/form/taf-table/div/div[3]/table/tbody[2]/tr/td[6]/div")
            print(result.get_attribute("title"))
        except Exception as err:
            print(err)
        finally:
            count += 1


# 打开所有门
def ip_controller():
    # 控制菜单
    driver.find_element(By.XPATH,
                        "/html/body/div[1]/taf-core-layout/div[2]/div[4]/taf-content/div/taf-rdum-cctv-content-panel/div/taf-rdum-dooraccess-monitor/div/div[2]/taf-rdum-dooraccess-datapoints/div/taf-tab/div/taf-tab-normal/div/div/ul/li[3]/a/uib-tab-heading/div/span").click()
    count = 1
    while True:
        try:
            driver.find_element(By.XPATH,
                                "/html/body/div[1]/taf-core-layout/div[2]/div[4]/taf-content/div/taf-rdum-cctv-content-panel/div/taf-rdum-dooraccess-monitor/div/div[2]/taf-rdum-dooraccess-datapoints/div/taf-tab/div/taf-tab-normal/div/div/div/div[3]/div/taf-rdum-dooraccess-control-tab/taf-table/div/div[3]/table/tbody[2]/tr[1]/table-actions/div/button").click()
            time.sleep(1)
            driver.find_element(By.XPATH,
                                "/html/body/div[1]/taf-core-layout/div[2]/div[4]/taf-content/div/taf-rdum-cctv-content-panel/div/taf-rdum-dooraccess-monitor/div/div[2]/taf-rdum-dooraccess-datapoints/div/taf-tab/div/taf-tab-normal/div/div/div/div[3]/div/taf-rdum-dooraccess-control-tab/taf-table/div/div[3]/table/tbody[2]/tr[1]/table-actions/div/ul/button").click()
            time.sleep(1)
            driver.find_element(By.XPATH,
                                "/html/body/div[1]/div/div/taf-modal-dialog/div/div[2]/taf-rdum-dooraccess-control-send-command/form/fieldset/fieldset/button[2]/div").click()
            time.sleep(3)
        except Exception as err:
            print(err)
            exit(0)
            ActionChains(driver).send_keys(Keys.ARROW_DOWN).perform()
            # time.sleep(1)
            # driver.find_element(By.XPATH,
            #                     "/html/body/div[1]/div/div/taf-modal-dialog/div/div[2]/taf-rdum-dooraccess-control-send-command/form/fieldset/fieldset/button[2]/div").click()
            # time.sleep(3)
        finally:
            count += 1
            print(count)


if __name__ == '__main__':
    login()
    clickButton()
    # click_ip_control_menu()

    # 点击门1的授权菜单
    click_door_auth_menu()

    auth_button()
    # sendOpenDoor()

    # 打开所有门
    # ip_controller()
